//
//  KnowMessageViewController.m
//  HealthA段
//
//  Created by MrJXT on 15/6/6.
//  Copyright (c) 2015年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "KnowMessageViewController.h"
#import "KnowMessage.h"
#import "KnowMessageCell.h"
#import "AFHTTPRequestOperationManager.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "KnowDatilViewController.h"
#import "Singleton.h"
#define HEIGHT self.view.frame.size.height
#define WIDTH self.view.frame.size.width
@interface KnowMessageViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *KnowMessageTableView;
@property(nonatomic,retain)NSMutableArray *knowMessageArr;
@property(nonatomic,assign)NSInteger page;
@end

@implementation KnowMessageViewController
- (void)dealloc
{
    [_knowMessageArr release];
    [_KnowMessageTableView release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.knowMessageArr = [NSMutableArray array];
        self.page = 1;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [[Singleton shareSingleton] hiddenNO];
    [self creatView];
    [self PleaseData];
//    [self pulldown];
//    [self pullup];
    [self.KnowMessageTableView.legendFooter beginRefreshing];
    [self method];
    
}

//navigationController.view 隐藏问题
//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    self.tabBarController.tabBar.hidden = NO;
//    self.tabBarController.tabBar.hidden = NO;
//}
//
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:animated];
//    self.tabBarController.tabBar.hidden = YES;
//}
//试图创建
-(void)creatView
{
    self.KnowMessageTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49) style:UITableViewStylePlain];
    self.KnowMessageTableView.rowHeight = 130;
    self.KnowMessageTableView.delegate = self;
    self.KnowMessageTableView.dataSource = self;
//    self.KnowMessageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.KnowMessageTableView];
    [_KnowMessageTableView release];
}
//协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.knowMessageArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *resue = @"reuse";
    KnowMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:resue];
    if (cell == nil) {
        cell = [[[KnowMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:resue]autorelease];
    }
    KnowMessage *km = self.knowMessageArr[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"http://www.yi18.net/%@",km.img];
    NSURL *url = [NSURL URLWithString:str];
    [cell.picView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"占位图"]];
    cell.titleLabel.text = km.title;
    cell.timeLAbel.text = [NSString stringWithFormat:@"时间:%@",km.time];
//    cell.authorLabel.text = km.author;
//    cell.countLabel.text = [NSString stringWithFormat:@"浏览:%@",km.count];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
//请求数据
-(void)PleaseData
{
    NSString *url = [NSString stringWithFormat:@"http://api.yi18.net/lore/list?page=%d&limit=10&id=%ld",self.page,(long)self.id];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"yi18"];
        for (NSMutableDictionary *tep in arr) {
            KnowMessage *km = [[KnowMessage alloc]init];
            [km setValuesForKeysWithDictionary:tep];
            [self.knowMessageArr addObject:km];
            [km release];
        }
        for (int i = 0; i < self.knowMessageArr.count; i++) {
            if ([self.knowMessageArr[i] img] == nil) {
                [self.knowMessageArr removeObjectAtIndex:i];
            }
        }
        if (self.knowMessageArr.count > 0) {
            [[Singleton shareSingleton]hiddenYes];
        }
        [self.KnowMessageTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"请求失败");
    }];
//    self.page++;
}
//下拉刷新
//-(void)pulldown
//{
//    [self.KnowMessageTableView addLegendHeaderWithRefreshingBlock:^{
//        [self.knowMessageArr removeAllObjects];
//        self.page = 1;
//        [self PleaseData];
//        [self.KnowMessageTableView reloadData];
//        [self.KnowMessageTableView.header endRefreshing];
//        
//    }];
//}
////上啦加载
//-(void)pullup
//{
//    [self.KnowMessageTableView addLegendFooterWithRefreshingBlock:^{
//        [self PleaseData];
//        [self.KnowMessageTableView reloadData];
//        [self.KnowMessageTableView.footer endRefreshing];
//        
//    }];
//}
//跳转详情页面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KnowDatilViewController *kvc = [[KnowDatilViewController alloc]init];
    KnowMessage *kmc = self.knowMessageArr[indexPath.row];
    kvc.id = kmc.id;
    kvc.title = kmc.title;
    [self.navigationController pushViewController:kvc animated:YES];
    [kvc release];
}
//加载刷新
- (void)method
{
    [self.KnowMessageTableView addLegendHeaderWithRefreshingBlock:^{
        [self.knowMessageArr removeAllObjects];
        self.page = 1;
        [self PleaseData];
        [self.KnowMessageTableView reloadData];
        [self.KnowMessageTableView.header endRefreshing];
    }];
    [self.KnowMessageTableView.legendHeader beginRefreshing];
    [self.KnowMessageTableView addLegendFooterWithRefreshingBlock:^{
        self.page++;
        [self PleaseData];
        [self.KnowMessageTableView reloadData];
        [self.KnowMessageTableView.footer endRefreshing];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
